# -*- coding: utf-8 -*-
import pandas as pd
from matplotlib import pyplot as plt
from sklearn.linear_model import LinearRegression as lr

def Temperature():
    # 读取所需数据文件
    temp = pd.read_csv('GlobalSurfaceTemperature.csv')
    co2  = pd.read_csv('CO2ppm.csv')
    gas  = pd.read_csv("GreenhouseGas.csv")
    '''
    补充代码：
    1. 查看数据文件结构。
    2. 读取数据并对缺失值处理
    3. 对时间序列数据集进行处理并重新采样
    4. 整理数据
    5. 使用 scikit-learn 预测
    6. 将预测结果按列表返回
    '''
    co2 = co2[co2.columns[1:]].set_index(pd.to_datetime(co2.Year.astype('str')))
    temp = temp[temp.columns[1:]].set_index(pd.to_datetime(temp.Year.astype('str')))
    gas = gas[gas.columns[1:]].set_index(pd.to_datetime(gas.Year.astype('str')))

    df = pd.concat([gas,co2,temp],axis = 1)
    gas_data = df.iloc[:,:4].fillna(method='ffill').fillna(method='bfill')
    data = gas_data['1970':'2010'].iloc[:,:4]#去除后面四列
    test = gas_data['2011':'2017'].iloc[:,:4]
    train_target = df['1970':'2010'];

    model = lr().fit(data,train_target.Upper)
    UpperPredict = model.predict(test)
    UpperPredict = list(map(lambda x: float(format(x,'.3f')), UpperPredict))

    model = lr().fit(data,train_target.Median)
    MedianPredict = model.predict(test)
    MedianPredict = list(map(lambda x: float(format(x,'.3f')), MedianPredict))

    model = lr().fit(data,train_target.Lower)
    LowerPredict = model.predict(test)
    # UpperPredict = [float(format(i,'.3f')) for i in UpperPredict]
    LowerPredict = list(map(lambda x: float(format(x,'.3f')), LowerPredict))
    # 将预测结果按 2011-2017 年份顺序，并保留 3 位小数后以列表形成储存
    # 按高、中、低依次返回预测结果列表
    return UpperPredict, MedianPredict, LowerPredict


if __name__=='__main__':
    print(Temperature())
#/home/shiyanlou/anaconda3/bin/python challenge7_4.py
